package com.helloandroid.activitys

import android.widget.Switch
import androidx.core.content.edit
import com.helloandroid.AppUtil
import com.helloandroid.MyApplication
import com.helloandroid.R
import com.helloandroid.ads.NativeAdUtil
import com.helloandroid.base.BaseActivity
import com.helloandroid.msg.Msg
import com.helloandroid.tools.AppConfig
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions
import dai.android.util.AndroidUtils

class NotifySettingActivity : BaseActivity() {

    lateinit var shResident :Switch
    lateinit var shHongBaoYu : Switch
    lateinit var shCanBu:Switch
    lateinit var shBuShu:Switch

    override fun getContentViewResId() = R.layout.activity_notify_setting

    override fun initView() {

        // init views
        shResident = findViewById(R.id.shResident)
        shHongBaoYu = findViewById(R.id.shHongBaoYu)
        shCanBu = findViewById(R.id.shCanBu)
        shBuShu = findViewById(R.id.shBuShu)

        findBtn(R.id.ibBack).setOnClickListener {
            finish()
        }

        // 读取通知设置
        // val pref = MyApplication.getPerference(AppConfig.Pref_Notify)
        shResident.isChecked = MyApplication.app.serverProxy!!.queryPrefFlag(Msg.C2S_Pref_Resident)
        shHongBaoYu.isChecked = MyApplication.app.serverProxy!!.queryPrefFlag(Msg.C2S_Pref_Hby)
        shCanBu.isChecked = MyApplication.app.serverProxy!!.queryPrefFlag(Msg.C2S_Pref_ChiFan)
        shBuShu.isChecked =  MyApplication.app.serverProxy!!.queryPrefFlag(Msg.C2S_Pref_BuShu)

        // 监听器
        shResident.setOnCheckedChangeListener { _, isChecked ->
            if (isChecked && !AndroidUtils.isNotificationEnabled(this)){
                XXPermissions.with(this)
                        .permission(Permission.NOTIFICATION_SERVICE)
                        .request { _, _ ->
                            AppUtil.startBgService()
                        }
            }
            AppUtil.app.serverProxy?.sendMsgToServer(Msg.C2S_Pref_Resident,isChecked.toString())
        }

        shHongBaoYu.setOnCheckedChangeListener { _, isChecked ->
            if (isChecked && !AndroidUtils.isNotificationEnabled(this)){
                XXPermissions.with(this)
                        .permission(Permission.NOTIFICATION_SERVICE)
                        .request { _, _ -> }
            }
            AppUtil.app.serverProxy?.sendMsgToServer(Msg.C2S_Pref_Hby,isChecked.toString())
        }

        shCanBu.setOnCheckedChangeListener { _, isChecked ->
            if (isChecked && !AndroidUtils.isNotificationEnabled(this)){
                XXPermissions.with(this)
                        .permission(Permission.NOTIFICATION_SERVICE)
                        .request { _, _ -> }
            }
            AppUtil.app.serverProxy?.sendMsgToServer(Msg.C2S_Pref_ChiFan,isChecked.toString())
        }
        shBuShu.setOnCheckedChangeListener { _, isChecked ->
            if (isChecked && !AndroidUtils.isNotificationEnabled(this)){
                XXPermissions.with(this)
                        .permission(Permission.NOTIFICATION_SERVICE)
                        .request { _, _ -> }
            }
            AppUtil.app.serverProxy?.sendMsgToServer(Msg.C2S_Pref_BuShu,isChecked.toString())
        }

        NativeAdUtil.showNativeAd(this,findViewById(R.id.nativeView))
    }

    override fun initData() {
    }

    override fun isUseFullScreenMode() = true
}